home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / progjour / 1988 / 04 / l2.asm < prev    next >
Assembly Source File  |  1988-04-03  |  4KB  |  159 lines

  1. ; *** Listing 2 ***
  2. ;
  3. ; Program to illustrate use of read mode 1 (color compare mode)
  4. ; to detect collisions in display memory. Draws a yellow line on a
  5. ; blue background, then draws a perpendicular green line until the
  6. ; yellow line is reached.
  7. ;
  8. ; By Michael Abrash 4/2/88
  9. ;
  10. stack    segment    word stack 'STACK'
  11.     db    512 dup (?)
  12. stack    ends
  13. ;
  14. VGA_SEGMENT    EQU    0a000h
  15. SCREEN_WIDTH    EQU    80    ;in bytes
  16. GC_INDEX    EQU    3ceh    ;Graphics Controller Index register
  17. SET_RESET    EQU    0    ;Set/Reset register index in GC
  18. ENABLE_SET_RESET EQU    1    ;Enable Set/Reset register index in GC
  19. COLOR_COMPARE    EQU    2    ;Color Compare register index in GC
  20. GRAPHICS_MODE    EQU    5    ;Graphics Mode register index in GC
  21. BIT_MASK    EQU    8    ;Bit Mask register index in GC
  22. ;
  23. code    segment    word 'CODE'
  24.     assume    cs:code
  25. Start    proc    near
  26.     cld
  27. ;
  28. ; Select graphics mode 10h.
  29. ;
  30.     mov    ax,10h
  31.     int    10h
  32. ;
  33. ; Fill the screen with blue.
  34. ;
  35.     mov    al,1        ;blue is color 1
  36.     call    SelectSetResetColor ;set to draw in blue
  37.     mov    ax,VGA_SEGMENT
  38.     mov    es,ax
  39.     sub    di,di
  40.     mov    cx,7000h
  41.     rep    stosb        ;the value written actually doesn't
  42.                 ; matter, since set/reset is providing
  43.                 ; the data written to display memory
  44. ;
  45. ; Draw a vertical yellow line.
  46. ;
  47.     mov    al,14        ;yellow is color 14
  48.     call    SelectSetResetColor ;set to draw in yellow
  49.     mov    dx,GC_INDEX
  50.     mov    al,BIT_MASK
  51.     out    dx,al        ;point GC Index to Bit Mask
  52.     inc    dx        ;point to GC Data
  53.     mov    al,10h
  54.     out    dx,al        ;set Bit Mask to 10h
  55.     mov    di,40        ;start in the middle of the top line
  56.     mov    cx,350        ;do full height of screen
  57. VLineLoop:
  58.     mov    al,es:[di]    ;load the latches
  59.     stosb            ;write next pixel of yellow line (set/reset
  60.                 ; provides the data written to display
  61.                 ; memory, and AL is actually ignored)
  62.     add    di,SCREEN_WIDTH-1 ;point to the next scan line
  63.     loop    VLineLoop
  64. ;
  65. ; Select write mode 0 and read mode 1.
  66. ;
  67.     mov    dx,GC_INDEX
  68.     mov    al,GRAPHICS_MODE
  69.     out    dx,al        ;point GC Index to Graphics Mode register
  70.     inc    dx        ;point to GC Data
  71.     mov    al,00001000b    ;bit 3=1 is read mode 1, bits 1 & 0=00
  72.                 ; is write mode 0
  73.     out    dx,al        ;set Graphics Mode to read mode 1,
  74.                 ; write mode 0
  75. ;
  76. ; Draw a horizontal green line, one pixel at a time, from left
  77. ; to right until color compare reports a yellow pixel is encountered.
  78. ;
  79. ; Draw in green.
  80. ;
  81.     mov    al,2        ;green is color 2
  82.     call    SelectSetResetColor ;set to draw in green
  83. ;
  84. ; Set color compare to look for yellow.
  85. ;
  86.     mov    dx,GC_INDEX
  87.     mov    al,COLOR_COMPARE
  88.     out    dx,al        ;point GC Index to Color Compare register
  89.     inc    dx        ;point to GC Data
  90.     mov    al,14        ;we're looking for yellow, color 14
  91.     out    dx,al        ;set color compare to look for yellow
  92.     dec    dx        ;point to GC Index
  93. ;
  94. ; Set up for quick access to Bit Mask register.
  95. ;
  96.     mov    al,BIT_MASK
  97.     out    dx,al        ;point GC Index to Bit Mask register
  98.     inc    dx        ;point to GC Data
  99. ;
  100. ; Set initial pixel mask and display memory offset.
  101. ;
  102.     mov    al,80h        ;initial pixel mask
  103.     mov    di,100*SCREEN_WIDTH
  104.                 ;start at left edge of scan line 100
  105. HLineLoop:
  106.     mov    ah,es:[di]    ;do a read mode 1 (color compare) read.
  107.                 ; This also loads the latches.
  108.     and    ah,al        ;is the pixel of current interest yellow?
  109.     jnz    WaitKeyAndDone    ;yes-we've reached the yellow line, so we're
  110.                 ; done
  111.     out    dx,al        ;set the Bit Mask register so that we
  112.                 ; modify only the pixel of interest
  113.     mov    es:[di],al    ;draw the pixel. The value written is
  114.                 ; irrelevant, since set/reset is providing
  115.                 ; the data written to display memory
  116.     ror    al,1        ;shift pixel mask to the next pixel
  117.     adc    di,0        ;advance the display memory offset if
  118.                 ; the pixel mask wrapped
  119.     jmp    HLineLoop
  120. ;
  121. ; Wait for a key to be pressed to end, then return to text mode and
  122. ; return to DOS.
  123. ;
  124. WaitKeyAndDone:
  125. WaitKeyLoop:
  126.     mov    ah,1
  127.     int    16h
  128.     jz    WaitKeyLoop
  129.     sub    ah,ah
  130.     int    16h    ;clear the key
  131.     mov    ax,3
  132.     int    10h    ;return to text mode
  133.     mov    ah,4ch
  134.     int    21h    ;done
  135. Start    endp
  136. ;
  137. ; Enables set/reset for all planes, and sets the set/reset color
  138. ; to AL.
  139. ;
  140. SelectSetResetColor    proc    near
  141.     mov    dx,GC_INDEX
  142.     push    ax        ;preserve color
  143.     mov    al,SET_RESET
  144.     out    dx,al        ;point GC Index to Set/Reset register
  145.     inc    dx        ;point to GC Data
  146.     pop    ax        ;get back color
  147.     out    dx,al        ;set Set/Reset register to selected color
  148.     dec    dx        ;point to GC Index
  149.     mov    al,ENABLE_SET_RESET
  150.     out    dx,al        ;point GC Index to Enable Set/Reset register
  151.     inc    dx        ;point to GC Data
  152.     mov    al,0fh
  153.     out    dx,al        ;enable set/reset for all planes
  154.     ret
  155. SelectSetResetColor    endp
  156. code    ends
  157.     end    Start
  158.  
  159.